﻿@model Bricks.Business.Models.Systems.Dictionary

@{
    ViewBag.Title = "数据字典";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

@*实现对EasyUI的DataGird控件操作的JS代码*@
<script type="text/javascript">
    $(function () {
        initTable();
    });
    function initTable(queryData) {
        $('#dictionaryGrid').datagrid({
            url: '/Dictionary/GetDictionaryList',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            rownumbers: true,
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: true,
            selectOnCheck: true,
            singleSelect: true,
            queryParams: queryData,
            columns: [[
                { title: '@Html.LabelFor(m => m.DictionaryName)', field: 'DictionaryName', width: 120, sortable: true },
                { title: '@Html.LabelFor(m => m.Tag)', field: 'Tag', width: 120, sortable: true },
                { title: '@Html.LabelFor(m => m.Description)', field: 'Description', width: 230, sortable: true },
                {
                    title: '@Html.LabelFor(m => m.IsDeleted)', field: 'IsDeleted', width: 60, sortable: true, align: 'center',
                    formatter: function (value, row, index) {
                        return $.GetYesOrNo(value);
                    }
                }
            ]],
            toolbar: '#dictionaryToolBar'
        });
    }

    var Dictionary = {
        //新增
        Add: function () {
            var typeRow = $("#dictionaryTypeGrid").datagrid("getSelected");
            if (typeRow) {
                Dictionary.Mutual("新增", "/Dictionary/Edit?dictionaryTypeID=" + typeRow["ID"]);
            }
            else {
                $.EasyuiSlide("请选择一个分类");
            }
        },

        //修改
        Edit: function () {
            var row = $("#dictionaryGrid").datagrid("getSelections");
            if (row.length == 1) {
                Dictionary.Mutual("修改", "/Dictionary/Edit?ID=" + row[0].ID);
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //实体操作
        Mutual: function (title, href) {
            $.ModelDialog({
                title: title, href: href, width: 280,
                handler: function () {
                    $.ModelForm({
                        validate: true, button: $(this),
                        id: "#dictionary-model",
                        url: "/Dictionary/EditPost",
                        success: function (result) { Dictionary.Reload(); }
                    });
                },
                onLoad: function () { Dictionary.FormInit(); }
            });
        },

        //弹窗初始化
        FormInit: function () {
            ////定义父级的机构树
            //$("#OrgID").combotree({
            //    width: 150, url: "/OrgInfo/GetOrgComboTree",
            //    //onLoadSuccess: function (node, data) {
            //    //    var nodeid = $("#ID").val();
            //    //    if (nodeid) {
            //    //        var etree = $("#OrgID").combotree("tree");
            //    //        etree.tree("remove", etree.tree('find', nodeid).target);
            //    //    }
            //    //}
            //});
            ////定义机构类型枚举下拉
            //$("#UserTypeNum").combobox({
            //    editable: false, width: 150, panelHeight: "auto",
            //    data: [
            //        { value: "0", text: "超级管理员" },
            //        //{ value: "1", text: "管理员" },
            //        { value: "2", text: "普通用户" }
            //    ]
            //});
        },

        //删除
        Delete: function () {
            var row = $("#dictionaryGrid").datagrid("getSelected");
            if (row) {
                $.messager.confirm('操作提示', "确认删除[ " + row.DictionaryName + " ]吗？", function (r) {
                    if (r) {
                        $.post("/Dictionary/DeleteDictory?ID=" + row.ID, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                //移除删除的数据
                                $.EasyuiSlide(result.Message);
                                Dictionary.Reload();
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });
            }
            else {
                $.EasyuiSlide("请选择一条记录");
            }
        },

        //启用
        Enable: function () {
            Dictionary.SetEnable("True");
        },

        //禁用
        Disable: function () {
            Dictionary.SetEnable("False");
        },

        //设置启用或禁用
        SetEnable: function (isEnable) {
            var row = $("#dictionaryGrid").datagrid("getSelected");
            if (row) {
                $.post("/Dictionary/SetEnableDictionary", { id: row.ID, isEnable: isEnable }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);
                        Dictionary.Reload();
                    }
                    else {
                        $.EasyuiMsg(result.Message);
                    }
                });
            }
            else { $.EasyuiSlide("请选择一条记录"); }
        },

        //向上
        Up: function () {
            Dictionary.Order("Up");
        },

        //向下
        Down: function () {
            Dictionary.Order("Down");
        },

        //向上或向下方法
        Order: function (order) {
            var row = $("#dictionaryGrid").datagrid("getSelected");
            if (row) {
                //id：识别当前记录；pid：识别涉及调整的记录集合；order：向上或向下
                $.post("/Dictionary/SetOrderDictionary", { id: row.ID, pid: row.PID, order: order }, function (result) {
                    if (result.ResultType == 0) {
                        $.EasyuiSlide(result.Message);  //成功则右下角提示
                        Dictionary.Reload();
                        //$("#dictionaryTypeGrid").datagrid("selectRecord", row.ID);  //重新选中对应ID的记录（此处不生效，原因不明，后续探究）
                    }
                    else {
                        $.EasyuiMsg(result.Message);  //失败则弹出框提示
                    }
                });
            }
            else { $.EasyuiSlide(nullrowmsg); }
        },

        //重新加载数据
        Reload: function () {
            $("#dictionaryGrid").datagrid("reload");
        }
    }
</script>

<!--面板（从数据库取得菜单名称和图标等）-->
<div class="easyui-panel" title="字典内容" fit="true">
    <table id="dictionaryGrid" fit="true"></table>
    @*<div id="dictionaryToolBar">
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Add()' data-options='plain:true,iconCls:"icon-application_add"'>新增</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Edit()' data-options='plain:true,iconCls:"icon-application_delete"'>修改</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Delete()' data-options='plain:true,iconCls:"icon-bell"'>删除</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Disable()' data-options='plain:true,iconCls:"icon-bank"'>禁用</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Enable()' data-options='plain:true,iconCls:"icon-bestseller"'>启用</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Up()' data-options='plain:true,iconCls:"icon-up"'>向上</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Down()' data-options='plain:true,iconCls:"icon-down"'>向下</a>
        <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Dictionary.Reload()' data-options='plain:true,iconCls:"icon-blue_lego"'>刷新</a>
    </div>*@
    <div id="dictionaryToolBar">@Html.Raw(ViewBag.ToolBarInfo)</div>
</div>
